package com.kalaqia.image.di.module;

import com.bumptech.glide.RequestBuilder;
import com.kalaqia.image.GlideLoader;
import com.kalaqia.image.library.IImageLoaderStrategy;
import com.kalaqia.image.library.ImageLoaderOptions;

import dagger.Module;
import dagger.Provides;
import io.reactivex.Observable;

/**
 * function:
 * project:kalaqia
 * describe:
 * created owner:kalaqia 421407393@qq.com
 * created date:2017/8/9 9:29
 */
//<editor-fold desc="version update log">
// update owner:kalaqia  421407393@qq.com
// update datetime:2017/8/9 9:29 
// update remark:
//</editor-fold>
@Module
public class GlideObservableModule {

    @Provides
    public Observable<ImageLoaderOptions> providerImageLoaderOptionsObservable(ImageLoaderOptions options) {
        return Observable.just(options);
    }

    @Provides
    public Observable<RequestBuilder> provideRequestBuilderObservable(
            RequestBuilder builder,
            Observable<GlideLoader> glideLoaderObservable,
            Observable<ImageLoaderOptions> optionsObservable
    ) {
        return Observable.zip(
                Observable.just(builder)
                , glideLoaderObservable
                , optionsObservable
                , (builder1, loader, options) -> loader.zip(builder1, options)
        );

    }

    @Provides
    public Observable<GlideLoader> provideObservable(final IImageLoaderStrategy strategy) {
        return Observable.create(observableEmitter -> {
            if (strategy instanceof GlideLoader) {
                observableEmitter.onNext((GlideLoader) strategy);
            }
        });
    }

}
